<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
class Models_BKCard extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'bkcard_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'bkcard';
        return parent::__construct($config); 
    }

    /**
     * Get all bkcard
     * 
     * @param array $condition
     * @param string|array $order
     * @param int $count
     * @param int $offset
     * @return array
     */
    public function getAllBKCard($condition = array(), $order = null, $count = null, $offset = null) {
    	   $select = $this->select()
    	   		   ->setIntegrityCheck(false)
    	   		   ->from(array('c' => $this->_name))
    	   		   ->join(array('cat' => $this->_prefix . 'bkcard_category'), 'c.bkcard_category_id = cat.bkcard_category_id',array('value'))
    	   		   ->where('cat.enabled = 1')
                   ->order($order)
                   ->limit($count, $offset);
           return $this->fetchAll($select)->toArray();
    }
    /**
     * Get bkcard by id
     * @param int $id
     * @return array
     */
    public function getBKCardById($id)
    {
    	$select = $this->select()
    			->setIntegrityCheck(false)
    			->from(array('c' => $this->_name))
    	   		->join(array('cat' => $this->_prefix . 'bkcard_category'), 'c.bkcard_category_id = cat.bkcard_category_id',array('value'))
    			->where('c.bkcard_id=?',$id);
    			
    	return current($this->fetchAll($select)->toArray());
    }
    /**
     * Get bkcard by code
     * @param string $code
     * @return Zend_Db_Table_Row
     */
    public function getBKCardByCode($code)
    {
    	$select = $this->select()
    			->setIntegrityCheck(false)
    			->from(array('c' => $this->_name))
    	   		->join(array('cat' => $this->_prefix . 'bkcard_category'), 'c.bkcard_category_id = cat.bkcard_category_id',array('value','enabled'))
    			->where('code=?',$code);
    	
    	return current($this->fetchAll($select)->toArray());
    }
    
    
	public function generateCode($length = 9) 
    {
        /**
         * String with 62 letters
         */
        $chars      = '1234567890';
        $activeCode = '';
        for ($i = 1; $i <= $length; $i++) {
            $activeCode .= $chars{rand(0, 9)};
        }
        return $activeCode;
    }
    
 	public function makeUpNumber( $_ )
    {
    	return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1.' , strrev( $_ ) ) );
    }
}